﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;
using GenerateDiagram;
using System.Threading;

namespace ToolGenerateDiagram
{
    public partial class DisplayAllClass : DockContent
    {
        public DisplayAllClass()
        {
            InitializeComponent();
        }
        public AbstractDatabase _Database;

        public void LoadList(AbstractDatabase database)
        {
            if (database.Tables == null)
                database.SetTables(database.Name);
            _Database = database;
            lvClasses.Items.Clear();
            foreach (var item in database.Tables)
            {
                if (item.RenderOption.CanRender)
                    lvClasses.Items.Add(item.Name + ".cs", 0);
            }
        }

        private AbstractTable FintTable(string name)
        {
            return _Database.Tables.Single(t => t.Name == name);
        }

        private void viewClassToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (lvClasses.SelectedItems.Count > 0)
            {
                string text = lvClasses.SelectedItems[0].Text.Substring(0, lvClasses.SelectedItems[0].Text.LastIndexOf('.'));
                var table = FintTable(text);

                if (table.Columns == null)
                    table.SetColumns(_Database.Name);

                var doc = FormManager.FindDocument(text + ".cs", this);
                if (doc == null)
                {
                    DisplayClass classDis = new DisplayClass();
                    classDis.DockAreas = WeifenLuo.WinFormsUI.Docking.DockAreas.Document;
                    classDis.LoadData(table.GenerateClass(_Database.NamaspaceRender, _Database.VersionRender, _Database.CanRenderForeignKey));
                    classDis.Text = text + ".cs";
                    classDis.Show(this.DockPanel);
                }
                else
                {
                    var newDoc = doc as DisplayClass;
                    newDoc.LoadData(table.GenerateClass(_Database.NamaspaceRender, _Database.VersionRender, _Database.CanRenderForeignKey));
                    doc.DockHandler.Activate();
                }
                MainForm main = this.ParentForm as MainForm;
                main.HandleToolstrip(this.DockPanel.DocumentsCount > 0);
            }
        }

        private void DisplayAllClass_Resize(object sender, EventArgs e)
        {
            lvClasses.Columns[0].Width = this.Width - 25;
        }

        private List<AbstractTable> _Tables = new List<AbstractTable>();
        private void saveAllToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (folderBrowserDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                _Tables = _Database.Tables;
                ProgressBarStutus status = new ProgressBarStutus();
                status.Show();
                status.StartThread(new ParameterizedThreadStart(WriteAllFile), status);
            }
        }



        private void WriteAllFile(object dialog)
        {
            var status = dialog as ProgressBarStutus;
            status.pbStatus.Maximum = lvClasses.Items.Count;
            status.pbStatus.Value = 0;
            IOHelper ioHelper = new IOHelper();

            foreach (AbstractTable table in _Tables)
            {
                if (table.Columns == null)
                    table.SetColumns(_Database.Name);
                status.SetValue("Writing file....", folderBrowserDialog1.SelectedPath + "\\" + table.Name + ".cs");
                status.lblPersent.Text = Math.Round(((double)status.pbStatus.Value / status.pbStatus.Maximum) * 100, 0) + "%";
                if (table.RenderOption.CanRender)
                {
                    if (ioHelper.WriteFile(table.GenerateClass(_Database.NamaspaceRender, _Database.VersionRender, _Database.CanRenderForeignKey), folderBrowserDialog1.SelectedPath + "\\" + table.Name + ".cs", false))
                        status.pbStatus.Value += 1;
                    else
                        FormManager.ShowMessageBox("Write file error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }

            }
            status.Close();
            FormManager.ShowMessageBox("Write " + _Tables.Count + " file completed", MessageBoxButtons.OK, MessageBoxIcon.Information);
            status._Thread.Abort();
        }

        private void saveSelectionClassToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (lvClasses.SelectedItems.Count == 0)
                FormManager.ShowMessageBox("Please select one lest item", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            else
            {
                foreach (ListViewItem item in lvClasses.SelectedItems)
                {
                    string text = item.Text.Substring(0, item.Text.LastIndexOf('.'));
                    var table = FintTable(text);
                    _Tables.Add(table);
                }
                if (folderBrowserDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    ProgressBarStutus status = new ProgressBarStutus();
                    status.Show();
                    status.StartThread(new ParameterizedThreadStart(WriteAllFile), status);
                }

                //IOHelper ioHelper = new IOHelper();
                //if (folderBrowserDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                //{
                //    foreach (ListViewItem item in lvClasses.SelectedItems)
                //    {
                //        string text = item.Text.Substring(0, item.Text.LastIndexOf('.'));
                //        var table = FintTable(text);

                //        if (table.Columns == null)
                //            table.SetColumns(_Database.Name);

                //        if (!ioHelper.WriteFile(table.GenerateClass(_Database.NamaspaceRender, _Database.VersionRender, _Database.CanRenderForeignKey), folderBrowserDialog1.SelectedPath + "\\" + table.Name + ".cs", false))
                //            FormManager.ShowMessageBox("Write file error", MessageBoxButtons.OK, MessageBoxIcon.Error);

                //    }
                //    FormManager.ShowMessageBox("Write file complete", MessageBoxButtons.OK, MessageBoxIcon.Information);
                //}
            }
        }

        private void contextMenuStrip1_Opening(object sender, CancelEventArgs e)
        {
            contextMenuStrip1.Items[1].Enabled = contextMenuStrip1.Items[3].Enabled = lvClasses.SelectedItems.Count > 0;
        }
    }
}
